#!/usr/bin/env python3
# encoding: utf-8
#
# Copyright (c) 2010 Doug Hellmann.  All rights reserved.
#
"""Show all matches for a list of patterns.
"""


import re


def test_patterns(text: str, patterns: list):
    """Given source text and a list of patterns, look for
    matches for each pattern within the text and print
    them to stdout.
    """
    # Look for each pattern in the text and print the results
    for pattern, desc in patterns:
        print("'{}' -> {}".format(pattern, desc))
        print("  '{}'".format(text))

        for match in re.finditer(pattern, text):
            s = match.start()
            e = match.end()
            substr = text[s:e]
            n_backslashes = text[:s].count('\\')
            prefix = '.' * (s + n_backslashes)
            print("  {}'{}'".format(prefix, substr))
        print()


if __name__ == '__main__':
    # 测试模式匹配字符串，a后面紧跟着一个b，即ab
    test_patterns('abbaaabbbbaaaaa', [('ab', "'a' followed by 'b'"), ])
